<?php

namespace App\Http\Controllers\Personal;

use App\Model\Leave;
use App\Model\Sch_desc;
use App\Model\Staff_task;
use App\Model\Stop_settle;
use App\Model\Users;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
//任务执行
class ImplementController extends CommonController
{
    //

    protected $Staff_task;
    protected $Users;
	protected $Stop_settle;
	protected $Leave;
	protected $Sch_desc;

    public function __construct()
    {
        parent::__construct();
        $this->Staff_task = new Staff_task();
        $this->Users = new Users();
	    $this->Stop_settle=new Stop_settle();
	    $this->Leave= new Leave();
	    $this->Sch_desc=new Sch_desc();
    }

//	接单列表
    public function task_index(Request $request)
    {
        $request->time = isset($request->time) ? $request->time : date("Y-m", time());
        $firstday = date('Y-m-01', strtotime($request->time)); //月初
        $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));//月末
        $where['uid'] = Auth::user()->id;
        $where['start_time'] = $firstday;
        $where['end_time'] = $lastday;
        try {
            $data = $this->Users->user_all_task($where);
            if (count($data['staffs'])) {
                $status = ['申请中', '接单失败', '等待排班', '进行中', '已结束', '任务终止'];
                $newArr = [];
                foreach ($data['staffs'] as $kk => $vv) {
                    if ($vv['staff_task']) {
                        $newArr[$kk]['station_name'] = $vv['staff_task']['store_station']['name'];
                        $newArr[$kk]['status'] = $vv['staff_task']['store_station']['status'] == 0 ? '短期有效' : '长期有效';
                        $newArr[$kk]['star_day'] = $vv['staff_task']['task']['start_day'];
                        $newArr[$kk]['end_day'] = $vv['staff_task']['task']['end_day'];
                        $newArr[$kk]['start_time'] = $vv['staff_task']['task']['start_time'];
                        $newArr[$kk]['end_time'] = $vv['staff_task']['task']['end_time'];
                        $newArr[$kk]['statu'] = $status[$vv['staff_task']['status'] - 1];
                        $newArr[$kk]['statu_code'] = $vv['staff_task']['status'];
                        $newArr[$kk]['staff_task_id'] = $vv['staff_task']['id'];
                    }
                }
                if (is_array($newArr) && count($newArr) > 0) {
                    $newArr = array_values($newArr);
                }
            } else {
                $newArr = [];
            }

        } catch (\Exception $exception) {
            $message = $exception->getMessage();
        }
        if (!isset($message)) {
            return $this->success($newArr);
        } else {
            return $this->failed($message);
        }
    }

//  结算列表
	public function settle_index(Request $request)
	{
		$request->time = isset($request->time) ? $request->time : date("Y-m", time());
		$firstday = date('Y-m-01', strtotime($request->time)); //月初
		$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));//月末
		$where['uid'] = Auth::user()->id;
		$where['start_time'] = $firstday;
		$where['end_time'] = $lastday;
		try {
			$data=$this->Stop_settle->lists($where);
			$newArr=[];
			foreach($data as $kk=>$vv){
				$newArr[$kk]['station_name']=$vv['staff_task']['task']['store_station']['name'];
				$task_status=$vv['staff_task']['task']['store_station']['status'];
				$status=$vv['is_agree'];
				if($task_status==0){
					$newArr[$kk]['task_status']='短期有效';
				}elseif($task_status==1){
					$newArr[$kk]['task_status']='长期有效';
				}
				if($vv['is_agree']=1){
					$newArr[$kk]['status']='申请中';
				}elseif($vv['is_agree']=2){
					$newArr[$kk]['status']='已同意';
				}elseif($vv['is_agree']=3){
					$newArr[$kk]['status']='不通过';
				}
				$newArr[$kk]['task_start']=$vv['staff_task']['task']['store_station']['start_time'];
				$newArr[$kk]['task_end']=$vv['staff_task']['task']['store_station']['end_time'];
				$newArr[$kk]['id']=$vv['id'];
//				return $data;
			}

		} catch (\Exception $exception) {
			$message = $exception->getMessage();
		}
		if (!isset($message)) {
			return $this->success($newArr);
		} else {
			return $this->failed($message);
		}
	}

//	结算详情
	public function settle_desc(Request $request){
		$id=$request->id;
		$data['task_status']=$request->task_status==1?'长期有效':'短期有效';
		$data['task_start']=$request->task_start;
		$data['task_end']=$request->task_end;
		$stop_settle=$this->Stop_settle->desc($id);
		$data['termination']=date("Y-m-d",strtotime($stop_settle->termination));
		$staff_task_id=$stop_settle['staff_task_id'];
		try{
//			中止时长
			$data['leave_hour']=$this->Leave->where(['staff_task_id'=>$staff_task_id])->sum('time');
//			中止天数
			$data['leave_day']=$this->Leave->leave_day($staff_task_id);
//			未签到天数
			$data['no_sign_day']=$this->Sch_desc->no_sign_staff_task($staff_task_id);
//			已签到天数
			$data['sign_day']=$this->Sch_desc->sign_staff_task($staff_task_id);
//			签到天数
			$data['sign']=count($data['sign_day']);
//			排班天数
			$data['sch_day']=$this->Sch_desc->sch_day($staff_task_id);
//			申请状态
			$data['is_agree']=$stop_settle->is_agree;
			$data['termination']=$stop_settle->termination;

		}catch(\Exception $exception){
			$message=$exception->getMessage();
		}
		if (!isset($message)) {
			return $this->success($data);
		} else {
			return $this->failed($message);
		}
	}
}
